Skip to content

Remove try-finally from @with_pool for inlining performance#32

Merged
mgyoo86 merged 15 commits intomasterfrom
perf/remove_try_finally
Mar 13, 2026
Merged

Remove try-finally from @with_pool for inlining performance#32
mgyoo86 merged 15 commits intomasterfrom
perf/remove_try_finally

Conversation

@mgyoo86
Copy link
Copy Markdown
Member

@mgyoo86 mgyoo86 commented Mar 13, 2026

Motivation

Julia cannot inline functions containing try-finally. This blocks the compiler from optimizing @inline @with_pool functions in hot loops.

Removing try-finally yields ~15-25% speedup on typical patterns (single/nested/multi-type), with larger gains on Julia 1.10 LTS. LLVM IR confirms the exception handler frame is completely eliminated.

What Changed

@with_pool now uses direct rewind insertion instead of try-finally:

  • rewind! inserted at every exit point: implicit return, explicit return, break, continue
  • Entry depth guard (while pool._current_depth > _entry_depth + 1) for deferred recovery when inner scope throws and outer catches
  • @goto safety: compile-time hard error on external jumps (set-difference analysis of goto targets vs local labels)

The old try-finally behavior is preserved as @safe_with_pool / @safe_maybe_with_pool.

API

Macro Strategy Inlinable Use case
@with_pool Direct rewind Yes Default — hot paths
@safe_with_pool try-finally No Exception safety required
@maybe_with_pool Direct rewind + runtime toggle Yes Conditional pooling
@safe_maybe_with_pool try-finally + runtime toggle No Exception-safe conditional

Exception Contract

Scenario @with_pool @safe_with_pool
Normal exit / return / break / continue Cleanup guaranteed Cleanup guaranteed
Inner throw + outer catch Deferred recovery via entry depth guard Immediate cleanup
Uncaught exception Pool corrupted (reset! needed) Cleanup guaranteed

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants